package com.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.entity.History;
import com.mapper.HistoryMapper;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class HistoryService {

    @Autowired
    private HistoryMapper historyMapper;

    public int insertHistory(History history){
        return historyMapper.insertHistory(history);
    }

    public List<History> selectAllHistory(){
        return historyMapper.selectAllHistory();
    }

    public List<History> selectHistoryByUid(String userid){
        return historyMapper.selectHistoryByUid(userid);
    }

    public History selectHistoryByUidAndCid(String userid, String commid){
        return historyMapper.selectHistoryByUidAndCid(userid, commid);
    }

    public int updateHistory(History history){
        return historyMapper.updateHistory(history);
    }

    public static List<String> getRecommends(String userid, List<History> historyList, int K, int N){

        String historyJson = JSON.toJSONString(historyList);

        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile("E:\\projects\\yibaiyuan\\src\\main\\resources\\py\\recommend.py");

        PyFunction pyFunction = interpreter.get("getRecommendCommodities", PyFunction.class);
        PyObject pyObj = pyFunction.__call__(new PyString(userid), new PyString(historyJson), new PyInteger(K), new PyInteger(N));

        List<String> recommendsId = JSON.parseObject(pyObj.toString(), new TypeReference<ArrayList<String>>(){});
        return recommendsId;

    }


}